/**
 * creater:han.wen@dhc.com.cn
 * create time:2016-06-13 09:47:01
 * describe:文字游戏
 */
APP.controller('BallGameController', ['$scope', 'BallGameService', '$interval', '$stateParams',
  function ($scope, BallGameService, $interval, $stateParams) {

    /***变量声明***/
    $scope.settingData = JSON.parse($stateParams.settingData);
    $scope.teamA = {};
    $scope.teamB = {};
    $scope.game = {
      detail: '',
      right: '',
      open: '',
      field: '法兰西球馆',
      city: '法国',
      weather: '晴',
      type: '友谊赛',
      time: 0
    };
    $scope.detail = [];
    var gamePeriod = 0;
    var gameNumber = 0;
    var gameSpeed = 1;
    var gameSpeedNumber = 0;
    var modelData = {
      team: {
        name: '',
        badge: '',
        level: ''
      },
      member: [{
        name: '',
        age: '',
        position: '',
        level: ''
      }],
      game: {
        score: 0,
        goal: [{
          name: '',
          time: ''
        }],
        assist: []
      }
    };
    /**
     * 初始化函数
     */
    $scope.init = function () {
      $scope.teamA = $scope.initData($scope.settingData.team[0]);
      $scope.teamB = $scope.initData($scope.settingData.team[1]);
      $scope.game.weather = $scope.settingData.weather;
      $scope.game.field = $scope.settingData.field;
      $scope.game.type = $scope.settingData.type;
      gameSpeed = $scope.settingData.time / 3;
      $scope.getGameInfo();
    };
    $scope.initData = function (team) {
      switch (team.name) {
        case '拜仁慕尼黑':
          modelData = {
            team: {
              name: '拜仁慕尼黑',
              badge: 'aaa',
              level: '92'
            },
            member: team.member,
            game: {
              score: 0,
              goal: [{
                name: '',
                time: ''
              }],
              assist: []
            }
          };
          break;
        case '巴塞罗那':
          modelData = {
            team: {
              name: '巴塞罗那',
              badge: 'bbb',
              level: '95'
            },
            member: team.member,
            game: {
              score: 0,
              goal: [{
                name: '',
                time: ''
              }],
              assist: []
            }
          };
          break;
      }
      return modelData;
    };

    /**
     * 登录方法
     */
    $scope.getGameInfo = function () {
      var intervalGame = $interval(function () {
        var team1 = $scope.teamA;
        var team2 = $scope.teamB;
        var game = $scope.game;
        if (gameNumber == 0 && gamePeriod == 0) {
          game.open = BallGameService.getRandom(2) == 0 ? team1 : team2;
          game.right = game.open;
        }
        if (game.time == 0) {
          var detail = BallGameService.getGameStart(gameNumber, team1, team2, game);
          game.detail = detail.data;
          gameNumber++;
          if (detail.sign) {
            gameNumber = 0;
            gamePeriod = 1;
            game.time++;
          } else {
            gamePeriod = 0;
          }
        } else if ((game.time < 45 && game.time > 0) || (game.time < 90 && game.time > 45)) {
          var selection = BallGameService.getCondition([0.4, 0.8, 0.9]);
          var detail = '';
          if (selection == 0) {
            detail = BallGameService.pass(game.right);
            game.detail = detail.data;
          } else if (selection == 1) {
            detail = BallGameService.move(game.right);
            game.detail = detail.data;
          } else if (selection == 2) {
            detail = BallGameService.kick(game.right);
            game.detail = detail.data;
            if (game.right == team1) {
              team1.game.score = detail.score;
              if (detail.member) {
                team1.game.goal.push({name: detail.member, time: game.time});
              }
            } else {
              team2.game.score = detail.score;
              if (detail.member) {
                team2.game.goal.push({name: detail.member, time: game.time});
              }
            }
          } else if (selection == 3) {
            detail = BallGameService.randomCase(team1, team2, game);
            game.detail = detail.data;
          }
          game.detail = '[' + game.right.team.name + ']' + game.detail;
          if (!detail.right) {
            game.right = game.right == team1 ? team2 : team1;
          }
        } else if (game.time == 45) {
          game.open = game.open == team1 ? team2 : team1;
          game.right = game.open;
          var detail = BallGameService.getGameMiddle(gameNumber, team1, team2, game);
          game.detail = detail.data;
          gameNumber++;
          if (detail.sign) {
            gameNumber = 0;
            gamePeriod = 3;
            game.time++;
          } else {
            gamePeriod = 2;
          }
        } else if (game.time == 90) {
          var detail = BallGameService.getGameEnd(gameNumber, team1, team2, game);
          game.detail = detail.data;
          gameNumber++;
          if (detail.sign) {
            gameNumber = 0;
            gamePeriod = 5;
            $interval.cancel(intervalGame);
          } else {
            gamePeriod = 4;
          }
        }
        $scope.game = game;
        $scope.teamA = team1;
        $scope.teamB = team2;
        $scope.detail.push($scope.game.detail);
        if ($scope.detail.length > 5) {
          $scope.detail.shift();
        }
      }, 2000);

      var intervalTime = $interval(function () {
        var game = $scope.game;
        if (gamePeriod == 1 || gamePeriod == 3) {
          if (gameSpeed == gameSpeedNumber) {
            gameSpeedNumber = 0;
            game.time++;
          } else {
            gameSpeedNumber++;
          }
        } else if (gamePeriod == 5) {
          $interval.cancel(intervalTime);
        }
        $scope.game.time = game.time;
      }, 2000);
    };


    /*
     $scope.init();//如果页面缓存则用此方法初始化
     */
    $scope.$on('$ionicView.beforeEnter', function () {
      $scope.init();
    });
  }
]);

/**
 * creater:han.wen@dhc.com.cn
 * create time:2016-06-13 09:47:01
 * describe:文字游戏
 */
APP.service('BallGameService', ['$http', 'UrlService', function ($http, UrlService) {
  /**
   * 获取比赛信息
   * @param period  时间段
   * @param number  第几个
   * @param team  球队
   * @param member  成员
   * @param score  成员
   */
  this.getGameInfo = function (period, number, team, member, score) {
    var character = new Array(5);
    var data = {
      detail: '',
      score: score
    };
    character[0] = ['观众朋友们大家好', '欢迎收看', '裁判和双方队员走向球场', team + '从右往左攻', '比赛开始'];
    character[1] = [member + '传球', member + '射门', '球进了'];
    character[2] = ['中场休息', '双方球员都做出了足够努力', '希望下半场比赛双方能有好的表现'];
    character[3] = [member + '传球', member + '射门', '哎呀可惜了，比分没有变化'];
    character[4] = ['比赛结束', '谢谢双方球员精彩发挥', '直播就到这里'];
    data.detail = character[period][number];
    if (period == 1 && number == 2) {
      data.score++;
    }
    return data;
  };
  /**
   * 获得开场信息
   * @param index  输出哪一条
   * @param team1  球队1
   * @param team2  球队2
   * @param game  比赛信息
   */
  this.getGameStart = function (index, team1, team2, game) {
    var detail = ['观众朋友们大家好', '马上为您带来一场' + game.type + '的直播', '这场比赛在' + game.field + '举行',
      '对阵双方是' + team1.team.name + '和' + team2.team.name,
      '裁判和双方队员走向球场', '由' + game.right.team.name + '开球', '上半场比赛开始'];
    var character = {
      sign: false,
      data: detail[index]
    };
    character.sign = index == detail.length - 1;
    return character;
  };
  /**
   * 获得中场信息
   * @param index  输出哪一条
   * @param team1  球队1
   * @param team2  球队2
   * @param game  比赛信息
   */
  this.getGameMiddle = function (index, team1, team2, game) {
    var detail = ['中场休息时间到了', '场上的比分为' + team1.game.score + ':' + team2.game.score];
    if (team1.game.score > team2.game.score) {
      detail.push(team2.team.name + '比分暂时落后');
    } else if (team1.game.score == team2.game.score) {
      detail.push('双方中场战平');
    } else if (team1.game.score < team2.game.score) {
      detail.push(team1.team.name + '比分暂时落后');
    }
    detail.push('双方球员都做出了足够努力');
    detail.push('希望下半场比赛双方能有好的表现');
    detail.push('回更衣室双方如何调整呢？我们拭目以待');
    detail.push('易边再战');
    detail.push('下半场比赛开始');
    detail.push(game.right.team.name + '先控球');
    var character = {
      sign: false,
      data: detail[index]
    };
    character.sign = index == detail.length - 1;
    return character;
  };
  /**
   * 获得终场信息
   * @param index  输出哪一条
   * @param team1  球队1
   * @param team2  球队2
   * @param game  比赛信息
   */
  this.getGameEnd = function (index, team1, team2, game) {
    var detail = ['全场比赛结束', '场上的比分为' + team1.game.score + ':' + team2.game.score];
    if (team1.game.score > team2.game.score) {
      detail.push(team1.team.name + '取得了胜利');
    } else if (team1.game.score == team2.game.score) {
      detail.push('双方握手言和');
    } else if (team1.game.score < team2.game.score) {
      detail.push(team2.team.name + '赢得了比赛');
    }
    detail.push('直播就到这里，再见');
    var character = {
      sign: false,
      data: detail[index]
    };
    character.sign = index == detail.length - 1;
    return character;
  };
  /**
   * 传球
   * @param team
   */
  this.pass = function (team) {
    var character = {
      right: true,
      data: ''
    };
    var random1 = this.getRandom(11);
    var random2 = this.getRandom(11, random1);
    var condition = this.getCondition([0.8]);
    var detail = condition == 0 ? this.goodMoment(0) : this.badMoment(0);
    character.data = team.member[random1].name + this.passType() + team.member[random2].name + ',' + detail;
    character.right = condition == 0;
    return character;
  };
  /**
   * 传球类型
   */
  this.passType = function () {
    var character1 = ['长传', '短传', '直塞', '挑传', '回做', '大脚', '传中', '分边',
      '45度斜长传', '长传冲掉', '短传渗透', '低平球传中', '横跨半场'];
    var character2 = ['传给了', '找到了', '发现了', '给到了'];
    return character1[this.getRandom(character1.length - 1)] + character2[this.getRandom(character2.length - 1)];
  };
  /**
   * 射门
   * @param team
   */
  this.kick = function (team) {
    var character = {
      score: team.game.score,
      member: '',
      right: true,
      data: ''
    };
    var random = this.getRandom(11);
    var condition = this.getCondition([0.2]);
    var detail = condition == 0 ? this.goodMoment(1) : this.badMoment(1);
    character.data = team.member[random].name + this.kickType() + ',' + detail;
    character.score = condition == 0 ? character.score + 1 : character.score;
    character.right = condition == 0;
    character.member = condition == 0 ? team.member[random].name : '';
    return character;
  };
  /**
   * 射门类型
   */
  this.kickType = function () {
    var character = ['射门', '推射', '抽射', '头球', '吊射', '弹射', '垫射', '捅射', '挑射', '脚弓推射',
      '正脚背抽地滚球', '脚内侧弧线球', '正脚背抽高球', '外脚背抽射', '正脚背凌空抽射', '甩头攻门',
      '高空轰炸', '鱼跃冲顶', '外脚背凌空抽射', '凌空垫射', '小腿弹射', '脚尖捅射', '勺子射门', '脚后跟射门',
      '剪刀腿射门', '倒勾射门', '凌空侧勾', '外脚背拨射', '倒地卧射'];
    return character[this.getRandom(character.length - 1)];
  };
  /**
   * 移动
   * @param team
   */
  this.move = function (team) {
    var character = {
      right: true,
      data: ''
    };
    var random = this.getRandom(11);
    var condition = this.getCondition([0.8]);
    var detail = condition == 0 ? this.goodMoment(2) : this.badMoment(2);
    character.data = team.member[random].name + this.moveType() + ',' + detail;
    character.right = condition == 0;
    return character;
  };
  /**
   * 移动类型
   */
  this.moveType = function () {
    var character = ['停球', '加速', '盘带', '趟球', '变向', '突破', '急停', '扣球变向', '人球分过', '急停变向',
      '马赛回旋', '牛尾巴过人', '钟摆过人', '彩虹过人', '后脚跟磕球', '正脚背抽高球'];
    return character[this.getRandom(character.length - 1)];
  };
  /**
   * 随机事件
   */
  this.randomCase = function (team1, team2, game) {
    var detail = ['皮球到了' + game.right.team.name + '这边', game.right.team.name + '由守转攻',
      '虽然是' + game.type + '但是两队都很拼啊', '场上发生了不和谐的因素,无球队员起了冲突,闹事者是' + team1.member[this.getRandom(11)].name + '和' + team2.member[this.getRandom(11)].name];
    var character = {
      right: true,
      data: detail[this.getRandom(detail.length - 1)]
    };
    return character;
  };
  /**
   * 精彩瞬间
   * @param type
   */
  this.goodMoment = function (type) {
    var character = [];
    character[0] = ['默契的配合', '精准的找到了队友', '撕开了对方的防守', '拉开了空间', '使得对手防守疲于奔命', '打乱了对方的节奏', '消耗了对手的体能', '打了对方一个措手不及'];
    character[1] = ['球进了!', '球进了进了进了!!!', '打进了一粒进球', 'Goal!!!', '球应声入网', '门将望球兴叹', '足球缓缓越过门线', '将球送入网窝', '突破了对手门将的十指关', '打破了场上的僵局'];
    character[2] = ['摆脱了对手的防守', '过掉了对方球员', '对位的球员无可奈何', '视防守的球员于无物', '撕开了整条后防线', '打开了边路的缺口'];
    return character[type][this.getRandom(character[type].length - 1)];
  };
  /**
   * 失意瞬间
   * @param type
   */
  this.badMoment = function (type) {
    var character = [];
    character[0] = ['接球队员没有准备好', '球给的稍微大了点', '传球力量稍小了些', '对手判断到了传球线路', '传出了界', '配合被对方识破', '传球时机没有掌握好'];
    character[1] = ['被对方守门员没收', '偏出了球门', '球打高了', '球打偏了', '就差一点点啊', '被门将单掌托了出去', '打中了横梁', '击中了门柱', '射门没有掌握好方向', '射门软绵绵的', '没有命中目标', '打了高射炮'];
    character[2] = ['失误了', '脚底一滑', '没有摆脱对手', '对方积极防守', '对手上了协防', '被对手判断对了方向'];
    return character[type][this.getRandom(character[type].length - 1)];
  };
  /**
   * 获取0到n之间的随机数
   * @param n  随机值的极限
   * @param m  随机值去重
   */
  this.getRandom = function (n, m) {
    var random = Math.floor(Math.random() * (n));
    if (m && m == random) {
      this.getRandom(n, m);
    }
    return random;//返回0-11之前的随机数
  };
  /**
   * 获取0到1之间的概率随机数
   * @param condition  条件
   */
  this.getCondition = function (condition) {
    var random = Math.random();
    if (condition.length == 1) {
      if (random >= 0 && random < condition[0]) {
        return 0;
      }
      return 1;
    }
    if (condition.length == 2) {
      if (random >= 0 && random < condition[0]) {
        return 0;
      } else if (random >= condition[0] && random < condition[1]) {
        return 1;
      }
      return 2;
    }
    if (condition.length > 2) {
      for (var i = 0; i < condition.length; i++) {
        if (i == 0) {
          if (random >= 0 && random < condition[i]) {
            return i;
          }
        } else if (i > 0 && i < condition.length - 1) {
          if (random >= condition[i - 1] && random < condition[i]) {
            return i;
          }
        }  else if (i == condition.length - 1) {
          if (random >= condition[i - 1] && random < condition[i]) {
            return i;
          } else {
            return i + 1;
          }
        }
      }
    }
  }
}]);
